{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/usr/anaconda2/lib/python2.7/site-packages/matplotlib/__init__.py:1350: UserWarning:  This call to matplotlib.use() has no effect\n",
      "because the backend has already been chosen;\n",
      "matplotlib.use() must be called *before* pylab, matplotlib.pyplot,\n",
      "or matplotlib.backends is imported for the first time.\n",
      "\n",
      "  warnings.warn(_use_error_msg)\n"
     ]
    }
   ],
   "source": [
    "import matplotlib\n",
    "import matplotlib.pyplot as plt\n",
    "matplotlib.use('Agg')\n",
    "%matplotlib inline\n",
    "import random as random\n",
    "import numpy as np\n",
    "import csv"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "x_data = [ 338.,  333.,  328. , 207. , 226.  , 25. , 179. ,  60. , 208.,  606.]\n",
    "y_data = [  640.  , 633. ,  619.  , 393.  , 428. ,   27.  , 193.  ,  66. ,  226. , 1591.]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "x = np.arange(-200,-100,1) #bias\n",
    "y = np.arange(-5,5,0.1) #weight\n",
    "Z =  np.zeros((len(x), len(y)))\n",
    "X, Y = np.meshgrid(x, y)\n",
    "for i in range(len(x)):\n",
    "    for j in range(len(y)):\n",
    "\t\tb = x[i]\n",
    "\t\tw = y[j]\n",
    "\t\tZ[j][i] = 0\n",
    "\t\tfor n in range(len(x_data)):\n",
    "\t\t\tZ[j][i] = Z[j][i] +  (y_data[n] - b - w*x_data[n])**2\n",
    "\t\tZ[j][i] = Z[j][i]/len(x_data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAZEAAAEPCAYAAACDTflkAAAABHN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0REkksRERGR\ngikiIiJSMEVEREQKpoiIiEjBFBERESmYIiIiIgVTREQiYmarzGydmT0d9ywi5ZKIz84SCYFz7hEz\nextwIu5ZRMpFKxGRaN1I5ocAiVQEfeyJSESyP9RnN5nPZPL6o+dFxkq7s0SicyOZiKw2sz7geuDj\nLvux9CIh0kpEJCJm9ndkfmb1bc65k2b2VeCUc+6eeCcTKR2tRESicyPwMZf5sasAE8hERSRYOrAu\nEoHs8ZBzgNzTe1eS+ZkNIsHSSkQkGguATc65bgAzOwe4GPhgrFOJlJhWIiLR2A7k/vjmLwJ/7Zxr\njmkekbLQgXWRiJjZx4BpwJuAg865r8Q8kkjJKSIiIlIw7c4SEZGCKSIiIlIwRURERAqmiIiISMEU\nERERKZgiIiIiBVNERESkYIqIiIgUTBEREZGC/X+MItbur+JN4QAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x7fc3733a7210>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# ydata = b + w * xdata \n",
    "b = -120 # initial b\n",
    "w = -4 # initial w\n",
    "lr = 1 # learning rate\n",
    "iteration = 100000\n",
    "\n",
    "b_lr = 0.0\n",
    "w_lr = 0.0\n",
    "\n",
    "# Store initial values for plotting.\n",
    "b_history = [b]\n",
    "w_history = [w]\n",
    "\n",
    "# Iterations\n",
    "for i in range(iteration):\n",
    "    \n",
    "    b_grad = 0.0\n",
    "    w_grad = 0.0\n",
    "    for n in range(len(x_data)):        \n",
    "        b_grad = b_grad  - 2.0*(y_data[n] - b - w*x_data[n])*1.0\n",
    "        w_grad = w_grad  - 2.0*(y_data[n] - b - w*x_data[n])*x_data[n]\n",
    "    \n",
    "    b_lr = b_lr + b_grad**2\n",
    "    w_lr = w_lr + w_grad**2\n",
    "    \n",
    "    # Update parameters.\n",
    "    b = b - lr/np.sqrt(b_lr) * b_grad \n",
    "    w = w - lr/np.sqrt(w_lr) * w_grad\n",
    "    \n",
    "    # Store parameters for plotting\n",
    "    b_history.append(b)\n",
    "    w_history.append(w)\n",
    "\n",
    "# plot the figure\n",
    "plt.contourf(x,y,Z, 50, alpha=0.5, cmap=plt.get_cmap('jet'))\n",
    "plt.plot([-188.4], [2.67], 'x', ms=12, markeredgewidth=3, color='orange')\n",
    "plt.plot(b_history, w_history, 'o-', ms=3, lw=1.5, color='black')\n",
    "plt.xlim(-200,-100)\n",
    "plt.ylim(-5,5)\n",
    "plt.xlabel(r'$b$', fontsize=16)\n",
    "plt.ylabel(r'$w$', fontsize=16)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
